import { createRouter, createWebHistory } from 'vue-router'

export const menu_router = [
  {
    path: '/',
    name: 'home',
    meta: {
      title: '首页'
    },
    component: () => import('../views/home/indexView.vue')
  },
  {
    path: '/room',
    name: 'room',
    meta: {
      title: '列表'
    },
    component: () => import('../views/room/indexView.vue')
  },
  {
    path: '/map',
    name: 'map',
    meta: {
      title: '地图'
    },
    component: () => import('../views/map/indexView.vue')
  },
  {
    path: '/pic',
    name: 'pic',
    meta: {
      title: '上传'
    },
    component: () => import('../views/pic/indexView.vue')
  },
  {
    path: '/customer',
    name: 'customer',
    meta: {
      title: '客服',
      roles: ['admin']
    },
    component: () => import('../views/customer/indexView.vue')
  },
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'layout',
      component: () => import('../views/layout/indexView.vue'),
      children: menu_router
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/login/indexView.vue')
    },
    {
      path: '/403',
      name: '403',
      component: () => import('../views/403/indexView.vue')
    },
    {
      path: '/:pathMatch(.*)*',
      component: () => import('../views/404/indexView.vue')
    }
  ]
})

router.beforeEach((to, form, next) =>{
  const role = localStorage.getItem('role')

  if(to.meta.roles && !to.meta.roles.includes(role)){
    next('/403')
  }
  next()
})

export default router
